<template>
  <div class="user-avator-dropdown">
    <Dropdown @on-click="handleClick">
      <Badge :dot="!!messageUnreadCount">
        <Avatar :src="userAvator"/>
        <span class="p-l-5 ml-10">{{userName}}</span>
      </Badge>
      <Icon :size="18" type="md-arrow-dropdown"></Icon>
      <DropdownMenu slot="list">
        <!-- <DropdownItem name="message">
          消息中心<Badge style="margin-left: 10px" :count="messageUnreadCount"></Badge>
        </DropdownItem> -->
        <DropdownItem name="revisepw">修改密码</DropdownItem>
        <DropdownItem name="logout">退出登录</DropdownItem>
        <!-- <DropdownItem name="windowRatio">
          选择显示比例
          <Icon type="ios-arrow-forward"></Icon>
        </DropdownItem> -->
        <Dropdown>
          <DropdownItem name="windowRatio">
            选择显示比例：{{windowRatio}}%
            <Icon type="ios-arrow-forward"></Icon>
          </DropdownItem>
          <DropdownMenu slot="list" class="pl-15 pr-15 line-h-33">
            <a class="display-block" @click="changeRatio(100)">100%</a>
            <a class="display-block" @click="changeRatio(95)">95%</a>
            <a class="display-block" @click="changeRatio(90)">90%</a>
            <a class="display-block" @click="changeRatio(85)">85%</a>
            <a class="display-block" @click="changeRatio(80)">80%</a>
            <a class="display-block" @click="changeRatio(75)">75%</a>
          </DropdownMenu>
        </Dropdown>
      </DropdownMenu>
    </Dropdown>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import './user.less'
import { mapActions } from 'vuex'
export default {
  name: 'User',
  props: {
    userAvator: {
      type: String,
      default: ''
    },
    userName: {
      type: String,
      default: ''
    },
    messageUnreadCount: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      windowRatio: null,
      ratioArr: [100, 95, 90, 85, 80, 75]
    }
  },
  mounted () {
    const cookieRatio = Cookies.get('changeRatio')
    if (cookieRatio) {
      var num = cookieRatio.replace(/[^0-9]/ig, '')
      this.windowRatio = +num
      console.log('🍇 this.windowRatio: ',this.windowRatio)
    } else {
      this.windowRatio = 100
    }
  },
  methods: {
    changeRatio (ratio) {
      console.log('改变显示比例 👍🍇 ratio: ',ratio)
      Cookies.set('changeRatio', JSON.stringify(ratio))
      this.windowRatio = ratio
      // this.$emit('on-zoom-change', ratio)
      var className = 'zoom-' + ratio
      document.getElementsByTagName("body")[0].className=className
    },
    ...mapActions([
      'handleLogOut'
    ]),
    logout () {
      this.handleLogOut().then(() => {
        this.$router.push({
          name: 'login'
        })
      })
    },
    message () {
      this.$router.push({
        name: 'message_page'
      })
    },
    revisepw () {
      this.$router.push({
        name: 'password'
      })
    },
    handleClick (name) {
      switch (name) {
        case 'logout': this.logout()
          break
        case 'message': this.message()
          break
        case 'revisepw': this.revisepw()
          break
        case 'windowRatio':
          break
      }
    }
  }
}
</script>
<style lang="less" scoped>
</style>

